/**
 * 统一的API响应格式工具
 */

class ApiResponse {
  constructor(success, code, message, data = null, error = null) {
    this.success = success;
    this.code = code;
    this.message = message;
    this.data = data;
    this.error = error;
    this.timestamp = new Date().toISOString();
  }
}

/**
 * 成功响应
 * @param {Response} res - Express响应对象
 * @param {*} data - 响应数据
 * @param {string} message - 响应消息
 * @param {number} statusCode - HTTP状态码
 */
const successResponse = (res, data, message = '操作成功', statusCode = 200) => {
  const response = new ApiResponse(true, statusCode, message, data);
  return res.status(statusCode).json(response);
};

/**
 * 错误响应
 * @param {Response} res - Express响应对象
 * @param {string} message - 错误消息
 * @param {number} statusCode - HTTP状态码
 * @param {*} error - 错误详情
 */
const errorResponse = (res, message = '操作失败', statusCode = 500, error = null) => {
  const response = new ApiResponse(false, statusCode, message, null, error);
  return res.status(statusCode).json(response);
};

/**
 * 验证错误响应
 * @param {Response} res - Express响应对象
 * @param {Array} errors - 验证错误数组
 */
const validationErrorResponse = (res, errors) => {
  const response = new ApiResponse(
    false, 
    400, 
    '参数验证失败', 
    null, 
    { validationErrors: errors }
  );
  return res.status(400).json(response);
};

/**
 * 分页响应
 * @param {Response} res - Express响应对象
 * @param {Array} data - 数据数组
 * @param {Object} pagination - 分页信息
 * @param {string} message - 响应消息
 */
const paginatedResponse = (res, data, pagination, message = '获取成功') => {
  const responseData = {
    list: data,
    pagination: {
      page: pagination.page,
      limit: pagination.limit,
      total: pagination.total,
      totalPages: Math.ceil(pagination.total / pagination.limit),
      hasNext: pagination.page < Math.ceil(pagination.total / pagination.limit),
      hasPrev: pagination.page > 1
    }
  };
  
  const response = new ApiResponse(true, 200, message, responseData);
  return res.status(200).json(response);
};

/**
 * 未找到资源响应
 * @param {Response} res - Express响应对象
 * @param {string} resource - 资源名称
 */
const notFoundResponse = (res, resource = '资源') => {
  const response = new ApiResponse(false, 404, `${resource}不存在`);
  return res.status(404).json(response);
};

/**
 * 未授权响应
 * @param {Response} res - Express响应对象
 * @param {string} message - 错误消息
 */
const unauthorizedResponse = (res, message = '未授权访问') => {
  const response = new ApiResponse(false, 401, message);
  return res.status(401).json(response);
};

/**
 * 禁止访问响应
 * @param {Response} res - Express响应对象
 * @param {string} message - 错误消息
 */
const forbiddenResponse = (res, message = '权限不足') => {
  const response = new ApiResponse(false, 403, message);
  return res.status(403).json(response);
};

/**
 * 冲突响应（资源已存在）
 * @param {Response} res - Express响应对象
 * @param {string} message - 错误消息
 */
const conflictResponse = (res, message = '资源已存在') => {
  const response = new ApiResponse(false, 409, message);
  return res.status(409).json(response);
};

/**
 * 服务器内部错误响应
 * @param {Response} res - Express响应对象
 * @param {*} error - 错误对象
 */
const internalErrorResponse = (res, error = null) => {
  const message = process.env.NODE_ENV === 'development' && error 
    ? error.message 
    : '服务器内部错误';
    
  const errorDetails = process.env.NODE_ENV === 'development' && error 
    ? { 
        message: error.message, 
        stack: error.stack 
      } 
    : null;

  const response = new ApiResponse(false, 500, message, null, errorDetails);
  return res.status(500).json(response);
};

module.exports = {
  successResponse,
  errorResponse,
  validationErrorResponse,
  paginatedResponse,
  notFoundResponse,
  unauthorizedResponse,
  forbiddenResponse,
  conflictResponse,
  internalErrorResponse
};